import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;

import javax.swing.JApplet;
import javax.swing.JFrame;


/**
 * @author worbunkel
 *
 */
public class Environment{
	private int width;
	private int height;
	private Drawer d;
	private Random generator;
	private Snake snake;
	private boolean isUpgrading = true;
	private int originalSleepTime = 100;
	private int sleepTime = 100;
	private Graphics g;
	private JFrame app;
	private int curTime;
	private int oldTime;
	private int delTime=0;
	
	public Environment(int width, int height){
		this.width=width;
		this.height=height;
		this.d = new Drawer(this);
		d.setFocusable(true);
		d.requestFocusInWindow();
		this.generator = new Random();
		this.snake = new Snake(this);
		app = new JFrame();
		app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		app.setResizable(false);
		app.add( d );
		app.setSize(width,height);
		app.setVisible(true);
		oldTime=(int) System.currentTimeMillis();
		setCurTime((int) System.currentTimeMillis());
	}
	
	

	public void update() {
		while(sleep()){}
		sleepTime=originalSleepTime;
		d.repaint();
	}
	
	public boolean sleep(){
		curTime=(int) System.currentTimeMillis();
		delTime = curTime-oldTime;
		if(delTime>sleepTime){
			oldTime=curTime;
			System.out.print("");
			return false;
		}
		else{
			return true;
		}
	}
	
	/**
	 * @return the d
	 */
	public Drawer getD() {
		return d;
	}



	/**
	 * @param d the d to set
	 */
	public void setD(Drawer d) {
		this.d = d;
	}



	public int randInt(int maxInt) {
		maxInt++;
		return generator.nextInt(maxInt);
	}
	
	/**
	 * @return the snake
	 */
	public Snake getSnake() {
		return snake;
	}

	/**
	 * @param snake the snake to set
	 */
	public void setSnake(Snake snake) {
		this.snake = snake;
	}
	
	
	
	
	
	
	//Getters and Setters
	
	/**
	 * @return the isUpgrading
	 */
	public boolean isUpgrading() {
		return isUpgrading;
	}

	/**
	 * @param isUpgrading the isUpgrading to set
	 */
	public void setUpgrading(boolean isUpgrading) {
		this.isUpgrading = isUpgrading;
	}

	/**
	 * @return the height
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * @param height the height to set
	 */
	public void setHeight(int height) {
		this.height = height;
	}

	/**
	 * @return the width
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * @param width the width to set
	 */
	public void setWidth(int width) {
		this.width = width;
	}



	/**
	 * @return the sleeptime
	 */
	public int getSleepTime() {
		return sleepTime;
	}



	/**
	 * @param sleeptime the sleeptime to set
	 */
	public void setSleepTime(int sleepTime) {
		this.sleepTime = sleepTime;
	}



	/**
	 * @return the curTime
	 */
	public int getCurTime() {
		return curTime;
	}



	/**
	 * @param curTime the curTime to set
	 */
	public void setCurTime(int curTime) {
		this.curTime = curTime;
	}
}
	
	
	
